package com.gz.thread.juc.volatileTest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Volatile可见性
 */
public class VolatileLook {
    private static  volatile int number = 0;

    public static void add() {
        number++;
    }

    public static void main(String[] args) {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors(),
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        try {
            poolExecutor.execute(() -> {
                while (number==0){}
                System.out.println("main线程改变number。。。");
            });
            TimeUnit.SECONDS.sleep(1);
            number = 1;

        } catch (Exception e) {
        } finally {
            poolExecutor.shutdown();
        }

    }
}

